/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.util;

import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.edi.db.EdiDb;

/**
 * 工具类处理解析出来的配置文件
 * 
 */
public final class PropertiesHelper {
	private static final Log log = LogFactory.getLog(PropertiesHelper.class);

	/**
	 * 返回一个属性对应的in值
	 * 
	 * @param propertyName
	 *            --属性名称
	 * @param properties
	 * @param defaultValue
	 * @return
	 */
	public static final int getInt(String propertyName, Properties properties,
			int defaultValue) {
		String value = extractPropertyValue(propertyName, properties);
		return value == null ? defaultValue : Integer.parseInt(value);
	}

	public static String extractPropertyValue(String propertyName,
			Properties properties) {
		String value = properties.getProperty(propertyName);
		if (value == null) {
			return null;
		}
		value = value.trim();
		if (StringHelper.isEmpty(value)) {
			return null;
		}
		return value;
	}

	/**
	 * 根据属性名称返回boolean值，默认false
	 * 
	 * @param propertyName
	 * @param properties
	 * @return
	 */
	public static boolean getBoolean(String propertyName, Properties properties) {
		return getBoolean(propertyName, properties, false);
	}

	public static boolean getBoolean(String propertyName,
			Properties properties, boolean defaultValue) {
		String value = extractPropertyValue(propertyName, properties);
		return value == null ? defaultValue : Boolean.valueOf(value)
				.booleanValue();
	}

	public static Integer getInteger(String propertyName, Properties properties) {
		String value = extractPropertyValue(propertyName, properties);
		return value == null ? null : Integer.valueOf(value);
	}

	public static Properties maskOut(Properties props, String key) {
		Properties clone = (Properties) props.clone();
		if (clone.get(key) != null) {
			clone.setProperty(key, "****");
		}
		return clone;
	}

	public static String getDialectClass(String dialect) {
		String returnVal = null;
		if (dialect == null || "".equals(dialect))
			return returnVal;
		if (EdiDb.DIALECT_MYSQL.equalsIgnoreCase(dialect))
			returnVal = "com.edi.db.dialect.MySQLDialect";
		if (EdiDb.DIALECT_MSSQL.equalsIgnoreCase(dialect))
			returnVal = "com.edi.db.dialect.SQLServerDialect";
		if (EdiDb.DIALECT_ORACLE.equalsIgnoreCase(dialect))
			returnVal = "com.edi.db.dialect.Oracle10gDialect";
		if (EdiDb.DIALECT_SQLITE.equalsIgnoreCase(dialect))
			returnVal = "com.edi.db.dialect.SQLServerDialect";
		if (EdiDb.DIALECT_SYBASE.equalsIgnoreCase(dialect))
			returnVal = "com.edi.db.dialect.SybaseDialect";
		return returnVal;
	}

	/**
	 * 根据必要元素构造数据库连接url
	 * 
	 * @param server
	 * @param dbname
	 * @param username
	 * @param password
	 * @param dialect
	 * @return
	 */
	public static String getDbUrl(String server, String dbname,
			String username, String password, String dialect) {
		String url = null;
		log.debug("server " + server);
		log.debug("dbname " + dbname);
		log.debug("username " + username);
		log.debug("password " + password);
		log.debug("dialect " + dialect);
		if (server == null || "".equals(server) || dialect == null
				|| "".equals(dialect))
			throw new RuntimeException("缺少构造数据库连接的必要元素！");
		if (EdiDb.DIALECT_MSSQL.equalsIgnoreCase(dialect))
			url = "jdbc:jtds:sqlserver://" + server + ";DatabaseName=" + dbname;
		if (EdiDb.DIALECT_MYSQL.equalsIgnoreCase(dialect))
			url = "jdbc:mysql://" + server + "/" + dbname;
		if (EdiDb.DIALECT_ORACLE.equalsIgnoreCase(dialect))
			url = "jdbc:oracle:thin:@" + server + ":" + dbname;
		if (EdiDb.DIALECT_SQLITE.equalsIgnoreCase(dialect))
			url = "jdbc:sqlite:EDI.db";
		if (EdiDb.DIALECT_SYBASE.equalsIgnoreCase(dialect))
			url = "jdbc:jtds:sybase://" + server + "/" + dbname;
		return url;
	}

	/**
	 * 根据数据库方言 返回默认数据库驱动
	 * 
	 * @param dialect
	 * @return
	 */
	public static String getDbDriver(String dialect) {
		if (dialect == null || "".equals(dialect))
			throw new RuntimeException("获取数据库驱动出现异常 dialect 为空");
		String driver = null;
		if (EdiDb.DIALECT_MSSQL.equalsIgnoreCase(dialect))
			driver = "net.sourceforge.jtds.jdbc.Driver";
		if (EdiDb.DIALECT_MYSQL.equalsIgnoreCase(dialect))
			driver = "com.mysql.jdbc.Driver";
		if (EdiDb.DIALECT_ORACLE.equalsIgnoreCase(dialect))
			driver = "oracle.jdbc.driver.OracleDriver";
		if (EdiDb.DIALECT_SQLITE.equalsIgnoreCase(dialect))
			driver = "org.sqlite.JDBC";
		return driver;
	}
}
